﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.ServiceFunctions.Data;
using System.Reflection;
using System.Data.SqlClient;

namespace GMS.MainClasses
{
    public partial class MainFormTemplate : Form
    {
        private ReturnData RD = new ReturnData();
        public MainFormTemplate()
        {
            InitializeComponent();
        }
        private String connectionString;
        public String ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }
        public DataSet ReturnDataSetDB(String ConnectionString, String Statement)
        {
            DataSet DS = RD.ReturnDataSetDB(ConnectionString, Statement);
            richTextBox1.AppendText(Statement);
            richTextBox1.AppendText("\r\n");
            return DS;
        }
        public DataSet ReturnDataSetDB(String ConnectionString, String Statement, String TableName)
        {
            DataSet DS = RD.ReturnDataSetDB(ConnectionString, Statement, TableName);
            richTextBox1.AppendText(Statement);
            richTextBox1.AppendText("\r\n");
            return DS;
        }
        public void ExecuteSqlCommand(String Statement, String ConnectionString)
        {
            RD.ExecuteSqlCommand(Statement, ConnectionString);
            richTextBox1.AppendText(Statement);
            richTextBox1.AppendText("\r\n");
        }
        public void ExecuteSqlCommand(String Statement, String ConnectionString, SqlTransaction Transaction, SqlConnection Connection)
        {
            RD.ExecuteSqlCommand(Statement, ConnectionString, Transaction, Connection);
            richTextBox1.AppendText(Statement);
            richTextBox1.AppendText("\r\n");
        }
        public virtual object ExecuteSqlCommandScalar(String SqlCommand, String ConnectionString)
        {
            object returnvalue;
            returnvalue = RD.ExecuteSqlCommandScalar(SqlCommand, ConnectionString);
            richTextBox1.AppendText(SqlCommand);
            richTextBox1.AppendText("\r\n");
            return returnvalue;
        }
        public virtual object ExecuteSqlCommandScalar(String SqlCommand, String ConnectionString, SqlTransaction Transaction, SqlConnection Connection)
        {
            object returnvalue;
            returnvalue = RD.ExecuteSqlCommandScalar(SqlCommand, ConnectionString, Transaction, Connection);
            richTextBox1.AppendText(SqlCommand);
            richTextBox1.AppendText("\r\n");
            return returnvalue;
        }
        public void AddMainMenuStrip(DataTable DT)
        {
            MenuStrip MS = new MenuStrip();

            CreateWindowMenu(MS,DT);

            MS.Dock = DockStyle.Top;
            this.MainMenuStrip = MS;
            this.Controls.Add(MS);
        }
        public void CreateWindowMenu(MenuStrip MenuStrip, DataTable DT)
        {
            ToolStripMenuItem WindowMenu = new ToolStripMenuItem();  
            foreach (DataRow DR in DT.Rows)
            {
                WindowMenu = new ToolStripMenuItem(DR["CaptionItem"].ToString());
                WindowMenu.Tag = DR["IdItem"];
                MenuStrip.Items.Add(WindowMenu);

                DataTable DT1 = new DataTable();
                DT1 = ReturnDataSetDB(ConnectionString, "exec SelctMenuItems '" + WindowMenu.Tag.ToString() + "'").Tables[0];

                if(DT1.Rows.Count!=0)
                WindowMenu.DropDownItems.Add(CreateItem(WindowMenu, DT1));

                if (WindowMenu.Text == "Окна")
                {
                    ((ToolStripDropDownMenu)(WindowMenu.DropDown)).ShowImageMargin = false;
                    ((ToolStripDropDownMenu)(WindowMenu.DropDown)).ShowCheckMargin = true;
                    MenuStrip.MdiWindowListItem = WindowMenu;
                }
            }
        }
        public ToolStripMenuItem CreateItem(ToolStripMenuItem MenuStrip, DataTable DT)
        {
            
            ToolStripMenuItem WindowMenu = new ToolStripMenuItem();    
            foreach (DataRow DR in DT.Rows)
            {
                if (DR[1].ToString() == "0")
                {
                    WindowMenu = new ToolStripMenuItem(DR["CaptionItem"].ToString());
                    WindowMenu.Tag = DR["IdForm"];
                }
                else
                {
                    WindowMenu = new ToolStripMenuItem(DR[0].ToString(), null, new EventHandler(MainMenu_Clicked));
                    WindowMenu.Tag = DR["IdItem"];
                }

                MenuStrip.DropDownItems.Add(WindowMenu);
                
                DataTable DT1 = new DataTable();
                DT1 = ReturnDataSetDB(ConnectionString, "exec SelctMenuItems '" + WindowMenu.Tag.ToString() + "'").Tables[0];

                CreateItem(WindowMenu, DT1);
            }
            return WindowMenu;      
        }
        public void MainMenu_Clicked(object sender, EventArgs e)
        {
            DataTable DT = ReturnDataSetDB(ConnectionString,"exec SelectAssembly "+sender.ToString()).Tables[0];
            object obj = new object();
            try
            {
                obj = Activator.CreateInstance(Assembly.LoadFrom(DT.Rows[0][0].ToString()).GetType(DT.Rows[0][1].ToString()));

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

            GMS.MainClasses.ChildForms CF = (GMS.MainClasses.ChildForms)obj;
            CF.ConnectionString = ConnectionString;
            CF.MdiParent = this;
            CF.Show();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ((ChildForms)ActiveMdiChild).ShowHideOptionPanel();
        }
        private void MainFormTemplate_FormClosing(object sender, FormClosingEventArgs e)
        {
            richTextBox1.AppendText("Остановка - " + DateTime.Now.ToString());
            richTextBox1.AppendText("\r\n");
            SqlConnection Connection = new SqlConnection(ConnectionString);
            //Connection.Open();
            foreach (String Line in richTextBox1.Lines)
            {
                ExecuteSqlCommand(ConnectionString, "AddUserLogs '" + Line.ToString().Replace("'", "*") + "'");
            }
            //Connection.Close();
            ToolStripManager.SaveSettings(this, "User");
        }

        private void MainFormTemplate_Load(object sender, EventArgs e)
        {
            ToolStripManager.LoadSettings(this, "User");
            richTextBox1.AppendText("Запуск - " + DateTime.Now.ToString());
            richTextBox1.AppendText("\r\n");
        }

        private void MainFormTemplate_MdiChildActivate(object sender, EventArgs e)
        {
            if (ActiveMdiChild == null)
                toolStripButton1.Enabled = false;
            else
                toolStripButton1.Enabled = true;
        }
    }
}
